package com.study.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.yygh.common.utils.MD5;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.yygh.common.result.Result;
import com.study.yygh.hosp.service.HospitalSetService;
import com.study.yygh.model.hosp.HospitalSet;
import com.study.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author tao
 * @create 2021-09-15 11:54
 * @Description
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin // 解决跨域问题 添加了网关配置解决跨域后就不需要加这个注解
public class HospitalSetController {

    // 注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    // 1、查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        // 调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    // 2、逻辑删除医院信息 ApiOperation描述一个类的一个方法 或者说一个接口
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    // 3、条件查询带分页
    //    a.创建vo类(HospitalSetQueryVo)，封装条件值
    //    b.编写controller获取条件对象分页数据(当前页，每页记录数)
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo){
        // 创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname(); // 医院名称
        String hoscode = hospitalSetQueryVo.getHoscode(); // 医院编号
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode )){
            wrapper.eq("hoscode",hoscode);
        }

        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        // 返回结果
        return Result.ok(pageHospitalSet);
    }


    // 4、添加医院设置 RequestBody 必须和post请求一起使用
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        // 设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        // 签名密匙
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+ random.nextInt(1000)));
        // 调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    // 5、根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
//        try {
//            // 模拟异常
//            int a = 1/0;
//        }catch (Exception e) {
//            throw new YyghException("自定义失败",500);
//        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 6、修改医院设置
    @ApiOperation("修改医院设置")
    @PutMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        // 源码 SqlHelper.retBool(getBaseMapper().updateById(entity)); 居然会自动取id
        if(flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 7、批量逻辑删除医院设置
    @ApiOperation("批量逻辑删除医院")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return Result.ok();
    }

    // 8、医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        // 根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 设置状态
        hospitalSet.setStatus(status);
        // 调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    // 9、发送签名密匙
    @ApiOperation("发送签名密匙")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO发送短信
        return Result.ok();
    }
}
